package jwj8508.j2ee.basic.util.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class MySQLDB {
	private static String DRV = "org.mariadb.jdbc.Driver";
	private static String URL = "jdbc:mariadb://localhost:3306/jwj8508?user=jwj8508&password=123456";
	
	public static Connection openConn() {
		try {
			Class.forName(DRV);
			return DriverManager.getConnection(URL);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Connection openCPConn() {
		try {
			Context initCtx = new InitialContext();
			Context ctx = (Context)initCtx.lookup("java:comp/env");			
			DataSource ds = (DataSource)ctx.lookup("jdbc/zipcode");			
			return ds.getConnection();
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return null;
	}
	
	public static void closeConn(Connection c, PreparedStatement p, ResultSet r) {
		if (r!=null) try {r.close();} catch(Exception e){}
		if (p!=null) try {p.close();} catch(Exception e){}
		if (c!=null) try {c.close();} catch(Exception e){}
	}	
}
